package com.qingmu.club.minaapi.controller.club;


import com.qingmu.club.minaapi.custom.logic.UsersWxLogic;
//import com.qingmu.club.minaapi.custom.service.CustomClubUsersService;
import com.qingmu.club.minaapi.custom.service.CustomClubUsersService;
import com.qingmu.club.minaapi.enums.DateTypeEnum;
import com.qingmu.club.minaapi.enums.RankingTypeEnum;
import com.qingmu.club.minaapi.plus.entity.ClubStaff;
import com.qingmu.club.minaapi.plus.entity.ClubUsers;
import com.qingmu.club.minaapi.plus.service.ClubService;
import com.qingmu.club.minaapi.plus.service.ClubStaffService;
import com.qingmu.club.minaapi.utils.DateUtil;
import com.qingmu.club.minaapi.utils.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 粉丝排行 前端控制器
 * </p>
 *
 * @author duke
 * @since 2019-08-20
 **/

@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(value = "粉丝排行(俱乐部端小程序)", description = "粉丝排行(俱乐部端小程序)")
@RequestMapping("/club/fans")
public class ClubFansController {


    @Autowired
    CustomClubUsersService customClubUsersService;

    @Autowired
    ClubService clubService;

    @Autowired
    ClubStaffService clubStaffService;

    @Autowired
    UsersWxLogic usersWxLogic;

    @RequestMapping(value = "/rankingList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "粉丝排行")
    public ResponseEntity rankingList(
            @ApiParam(required = true, value = "人员id") @RequestParam(required = true) String staffId,
            @ApiParam(required = true, value = "俱乐部id") @RequestParam(required = true) String clubId,
            @ApiParam(required = true, value = "统计纬度") @RequestParam(required = true) DateTypeEnum dateType,
            @ApiParam(required = true, value = " PERSON个人 SHOP门店") @RequestParam(required = true) RankingTypeEnum rankingType
    ) {
        ClubStaff clubStaff =this.usersWxLogic.getClubStaff(staffId);
        if(clubStaff==null){
            return ResponseObject.error(401,"人员信息找不到");
        }
        Date date = new Date();
        String year= new SimpleDateFormat("yyyy").format(date);//年
        String month= new SimpleDateFormat("yyyy-MM").format(date);//月
        HashMap<String,Object> week = DateUtil.getFristAndLashOfWeekDay(date);//周
        HashMap<String,Object> quarter = DateUtil.getFristAndLashOfDuarterDay(date);//季度

        Map<String, Object> params = new HashMap<>();
        List<Map<String,Object>> mapList = null;
        params.put(ClubUsers.CLUB_ID,clubId);
        /********************门店粉丝*********************/
        if(rankingType.equals(RankingTypeEnum.SHOP)) {

            if (dateType.equals(DateTypeEnum.YEAR)) { //年排行
                params.put(ClubUsers.CREATE_TIME, year);
                mapList = customClubUsersService.yearShopRankingList(params);

            } else if (dateType.equals(DateTypeEnum.QUARTER)) {//季度排行

                params.put("startDate", quarter.get("startDate"));
                params.put("endDate", quarter.get("endDate"));
                mapList = customClubUsersService.quarterShopRankingList(params);

            } else if (dateType.equals(DateTypeEnum.MONTH)) {//月排行

                params.put(ClubUsers.CREATE_TIME, month);
                mapList = customClubUsersService.monthShopRankingList(params);

            } else if (dateType.equals(DateTypeEnum.WEEK)) {//周排行

                params.put("startDate", week.get("startDate"));
                params.put("endDate", week.get("endDate"));
                mapList = customClubUsersService.weekShopRankingList(params);

            } else {

                mapList = customClubUsersService.sumShopRankingList(params); //总排行

            }
        }

        /********************个人粉丝*********************/
        if(rankingType.equals(RankingTypeEnum.PERSON)) {

            if (dateType.equals(DateTypeEnum.YEAR)) { //年排行

                params.put(ClubUsers.CREATE_TIME, year);
                mapList = customClubUsersService.yearPersonRankingList(params);
            } else if (dateType.equals(DateTypeEnum.QUARTER)) {//季度排行

                params.put("startDate", quarter.get("startDate"));
                params.put("endDate", quarter.get("endDate"));
                mapList = customClubUsersService.quarterPersonRankingList(params);
            } else if (dateType.equals(DateTypeEnum.MONTH)) {//月排行

                params.put(ClubUsers.CREATE_TIME, month);
                mapList = customClubUsersService.monthPersonRankingList(params);
            } else if (dateType.equals(DateTypeEnum.WEEK)) {//周排行

                params.put("startDate", week.get("startDate"));
                params.put("endDate", week.get("endDate"));
                mapList = customClubUsersService.weekPersonRankingList(params);
            } else {
                mapList = customClubUsersService.sumPersonRankingList(params); //总排行
            }
        }
        return ResponseObject.success(mapList);
    }

}
